<template>
  <div id="history" style="width: 100%; justify-content: center; display: flex">
     
    <div style="width: 100%">
     
      <div>
            <hte-upload    dbKey="Trd" ftpKey="ftp"
              busField="library"
              busType="1" 
            :show-file-list="false"
          :uploadDisabled="true"
          ref="upload"
          >
          </hte-upload>
        <a-table
          :columns="hteFile.authority3 == 1 ? columns : columns2"
          :data-source="attachList"
          rowKey="id"
          bordered
          :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          :pagination="ipagination"
          @change="handleTableChange"
        >
          <span slot="fileName" slot-scope="text, record">
            <a icon="download" @click="handlePreview(record)">{{ text }}</a>
          </span>
           
           <span slot="contentFlag" slot-scope="text, record" >

            {{text == 1 ?'是':'否'}}
            
          </span>
          <span slot="action" slot-scope="text, record">
            <a icon="download" @click="handleDownload(record)">下载</a>
          </span>
        </a-table>
      
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { getAction } from '@/api/manage'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import cloneDeep from 'lodash/cloneDeep'
import { formatFileSize, endWith } from '@/utils/common.js'
import '@/config/hteupload.install.js';
const columns = [
  {
    title: '附件名称',
    dataIndex: 'fileName',
    key: 'fileName',
    scopedSlots: { customRender: 'fileName' },
   width: '40%',
  },
  {
    title: '文件大小',
   
     customRender:function (text) {
      var size = !text?0:text;
        return formatFileSize(size)
      },
      dataIndex: 'size',
    key: 'size',
     width: '15%',
  },
  {
    title: '上传时间',
    dataIndex: 'uploadTime',
    key: 'uploadTime',
    width: '15%',
  },
  {
    title: '正文附件',
    dataIndex: 'contentFlag',
    key: 'contentFlag',
    width: '10%',
    scopedSlots: { customRender: 'contentFlag' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    //align: 'center',
    //fixed: 'right',
     width: '20%',
    scopedSlots: { customRender: 'action' },
  },
]
const columns2 = [
  {
    title: '附件名称',
    dataIndex: 'fileName',
    key: 'fileName',
    scopedSlots: { customRender: 'fileName' },
  },
  {
    title: '文件大小',
    dataIndex: 'size',
    key: 'size',
    customRender:function (text) {
      var size = !text?0:text;
        return formatFileSize(size)
      },
  },
  {
    title: '上传时间',
    dataIndex: 'uploadTime',
    key: 'uploadTime',
  }
]
export default {
  components: {},
  props: {
    hteFile: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      columns,
      columns2,
      attachList: [],
      selectedRowKeys: [],
      url: {
        list: '/lib/attach/list',
      },
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        id: 0,
        total: 0,
      },
      fileId: 0,
      version: 0,
    }
  },
  methods: {
    listAttachment(fileId, version) {
      this.fileId = fileId
      this.version = version
      this.ipagination.id = fileId
      this.ipagination.version = version
      let that = this
      let param = Object.assign({},this.ipagination);
      delete param.pageSizeOptions
      delete param.showTotal
      var p = new Promise(function(resolve, reject){
        getAction(that.url.list, param).then((res) => {
          if (res.success) {
            that.attachList = res.result.records || res.result
            resolve(res.result.records || res.result)
            if (res.result.total) {
              that.ipagination.total = res.result.total
              
            } else {
              that.ipagination.total = 0
            }
            that.$emit("getTotal",that.ipagination.total)
          }
        })
      })
      return p
    },
    onSelectChange(selectedRowKeys) {
      console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    handleDownload(row) {
      let that = this
      let record = cloneDeep(row) 
      record.id = record.attachId
      that.$refs.upload.download(0,[record])
    },
    handlePreview(row) {
        let that = this
      let record = cloneDeep(row) 
      record.id = record.attachId
      that.$refs.upload.handlePreview(record)

    },
    /**
     * 表结构切换页码
     */
    handleTableChange(pagination, filters, sorter) {
      this.ipagination = pagination
      this.ipagination.pageNo = this.ipagination.current
      this.listAttachment(this.fileId, this.version)
    },
  },
}
</script>

<style>
</style>